package org.xfy.ymcc.consumer;

import com.alibaba.fastjson.JSONObject;
import jdk.nashorn.internal.parser.JSONParser;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.xfy.common.constant.Constants;
import org.xfy.common.constant.RocketConstants;
import org.xfy.ymcc.domain.PayOrder;
import org.xfy.ymcc.service.IPayOrderService;

import java.io.UnsupportedEncodingException;

@Component
@RocketMQMessageListener(consumerGroup = "service-pay-consumer",
        //Topic
        topic = RocketConstants.ORDER_PAY_GROUP_TOPIC,
        //TAGS
        selectorExpression = RocketConstants.ORDER_PAY_GROUP_TAG
)
public class PayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IPayOrderService payOrderService;

    @Override
    @Transactional
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String s = new String(body, Constants.UTF8);
            PayOrder payOrder = JSONObject.parseObject(s, PayOrder.class);
            payOrderService.saveOrUpdate(payOrder);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
